package com.tools.wenjuanxing.service;

import com.tools.wenjuanxing.tools.selemiumApi;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class auto_service {
    Random random = new Random();
    String url = "https://www.wjx.cn/vm/wyj1KqP.aspx";
    public void auto_wenjuanxing() throws Exception{
        WebDriver driver = selemiumApi.driver(url,"chromedriver","问卷星");
        //答卷逻辑start----------------------------------------------------------------------------------
        //年级  1--大一 2--大二 3--大三  4--大四
        int i1 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div1\"]/div[2]/div["+i1+"]/span")).click();
        //性别 1--男  2-女
        int i2 = random.nextInt(2)+1;
        driver.findElement(By.xpath("//*[@id=\"div2\"]/div[2]/div["+i2+"]/span")).click();
        //户籍 1--农村  2-城镇
        int i3 = random.nextInt(2)+1;
        driver.findElement(By.xpath("//*[@id=\"div3\"]/div[2]/div["+i3+"]/span")).click();
        //月生活费单选共四条
        int i4 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div4\"]/div[2]/div["+i4+"]/span")).click();
        //经济状况单选共三条
        int i5 = random.nextInt(3)+1;
        driver.findElement(By.xpath("//*[@id=\"div5\"]/div[2]/div["+i5+"]/span")).click();
        //生活费够用吗单选共三条
        int i6 = random.nextInt(3)+1;
        driver.findElement(By.xpath("//*[@id=\"div6\"]/div[2]/div["+i6+"]/span")).click();
        //外卖食堂选择单选共三条
        int i7 = random.nextInt(3)+1;
        driver.findElement(By.xpath("//*[@id=\"div7\"]/div[2]/div["+i7+"]/span")).click();
        //外出聚餐频率单选共五条
        int i8 = random.nextInt(5)+1;
        driver.findElement(By.xpath("//*[@id=\"div8\"]/div[2]/div["+i8+"]/span")).click();
        //平时选择大牌or平价单选共两条
        int i9 = random.nextInt(2)+1;
        driver.findElement(By.xpath("//*[@id=\"div9\"]/div[2]/div["+i9+"]/span")).click();
        //平时逛商场、小店还是网店单选共三条
        int i10 = random.nextInt(3)+1;
        driver.findElement(By.xpath("//*[@id=\"div10\"]/div[2]/div["+i10+"]/span")).click();
        //购置新衣服频率单选共四条
        int i11 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div11\"]/div[2]/div["+i11+"]/span")).click();
        //平时出行选择单选共四条
        int i12 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div12\"]/div[2]/div["+i12+"]/span")).click();
        //生活费计划单选共四条
        int i13 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div13\"]/div[2]/div["+i13+"]/span")).click();
        //自身经济满意度单选共四条
        int i14 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div14\"]/div[2]/div["+i14+"]/span")).click();
        //自身经济支配满意度单选共四条
        int i15 = random.nextInt(4)+1;
        driver.findElement(By.xpath("//*[@id=\"div15\"]/div[2]/div["+i15+"]/span")).click();
        //支出有无记账单选共四条
        int i16 = random.nextInt(4)+1;//*[@id="div17"]/div[2]/div[1]
        driver.findElement(By.xpath("//*[@id=\"div16\"]/div[2]/div["+i16+"]/span")).click();
        //餐厅选择多选共四条
        boolean b17_1 = random.nextBoolean();
        if (b17_1 == true){
            driver.findElement(By.xpath("//*[@id=\"div17\"]/div[2]/div[1]/span")).click();
        }
        boolean b17_2 = random.nextBoolean();
        if (b17_2 == true){
            driver.findElement(By.xpath("//*[@id=\"div17\"]/div[2]/div[2]/span")).click();
        }
        boolean b17_3 = random.nextBoolean();
        if (b17_3 == true){
            driver.findElement(By.xpath("//*[@id=\"div17\"]/div[2]/div[3]/span")).click();
        }
        boolean b17_4 = random.nextBoolean();
        if (b17_4 == true){
            driver.findElement(By.xpath("//*[@id=\"div17\"]/div[2]/div[4]/span")).click();
        }
        if (b17_1==false && b17_2==false && b17_3==false && b17_4==false){
            driver.findElement(By.xpath("//*[@id=\"div17\"]/div[2]/div[4]/span")).click();
        }
        //购买时经济不足怎么做多选共五条
        boolean b18_1 = random.nextBoolean();
        if (b18_1 == true){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[1]/span")).click();
        }
        boolean b18_2 = random.nextBoolean();
        if (b18_2 == true){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[2]/span")).click();
        }
        boolean b18_3 = random.nextBoolean();
        if (b18_3 == true){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[3]/span")).click();
        }
        boolean b18_4 = random.nextBoolean();
        if (b18_4 == true){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[4]/span")).click();
        }
        boolean b18_5 = random.nextBoolean();
        if (b18_5 == true){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[5]/span")).click();
        }
        if (b18_1==false && b18_2==false && b18_3==false && b18_4==false && b18_5==false){
            driver.findElement(By.xpath("//*[@id=\"div18\"]/div[2]/div[5]/span")).click();
        }
        //恋爱花费多选共五条
        boolean b19_1 = random.nextBoolean();
        if (b19_1 == true){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[1]/span")).click();
        }
        boolean b19_2 = random.nextBoolean();
        if (b19_2 == true){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[2]/span")).click();
        }
        boolean b19_3 = random.nextBoolean();
        if (b19_3 == true){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[3]/span")).click();
        }
        boolean b19_4 = random.nextBoolean();
        if (b19_4 == true){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[4]/span")).click();
        }
        boolean b19_5 = random.nextBoolean();
        if (b19_5 == true){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[5]/span")).click();
        }
        if (b19_1==false && b19_2==false && b19_3==false && b19_4==false && b19_5==false){
            driver.findElement(By.xpath("//*[@id=\"div19\"]/div[2]/div[5]/span")).click();
        }
        //其他消费对象多选共五条
        boolean b20_1 = random.nextBoolean();
        if (b20_1 == true){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[1]/span")).click();
        }
        boolean b20_2 = random.nextBoolean();
        if (b20_2 == true){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[2]/span")).click();
        }
        boolean b20_3 = random.nextBoolean();
        if (b20_3 == true){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[3]/span")).click();
        }
        boolean b20_4 = random.nextBoolean();
        if (b20_4 == true){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[4]/span")).click();
        }
        boolean b20_5 = random.nextBoolean();
        if (b20_5 == true){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[5]/span")).click();
        }
        if (b20_1==false && b20_2==false && b20_3==false && b20_4==false && b20_5==false){
            driver.findElement(By.xpath("//*[@id=\"div20\"]/div[2]/div[5]/span")).click();
        }
        //答卷逻辑end---------------------------------------------------------------------------------
        //延长答卷时间
        Thread.sleep(2000);
        //提交
        driver.findElement(By.id("ctlNext")).click();

        //智能验证
        try {
            Thread.sleep(1000);
            if (driver.findElement(By.xpath("//*[@id=\"pop_box_msg\"]")) != null){
                driver.findElement(By.xpath("//*[@id=\"alert_box\"]/div[2]/div[2]/button")).click();
                driver.findElement(By.xpath("//*[@id=\"rectMask\"]")).click();
                Thread.sleep(2000);
                System.out.println("点击验证成功...");
            }
        }catch (Exception e){
            System.out.println("未点击验证...");
        }
        //滑动验证
        try {
            //等待滑动验证
            Thread.sleep(1000);
            if (driver.findElement(By.xpath("//*[@id=\"SM_BTN_1\"]/div[1]")) != null){
                WebElement webElement2 = driver.findElement(By.xpath("//*[@id=\"SM_BTN_1\"]/div[1]"));
                WebElement webElement3 = driver.findElement(By.xpath("//*[@id=\"SM_POP_CLOSE_1\"]"));

                Actions actions = new Actions(driver);
                //                                           ↓后面两个方法意思为执行actions
                actions.dragAndDrop(webElement2,webElement3).build().perform();
                System.out.println("滑动验证成功...");
            }
        }catch (Exception e){
            System.out.println("本次未使用滑动验证...");
        }
        //等待提交完成
        Thread.sleep(1000);
        //关闭浏览器
        driver.quit();


    }
}
